from rest_framework import serializers
from order import models

class CouponSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Coupon
        fields = '__all__'


class UserCouponSerializer(serializers.ModelSerializer):
    coulist=serializers.SerializerMethodField(read_only=True)
    def get_coulist(self,obj):
        return{"id":obj.coupon.id,"name":obj.coupon.name,"condition":obj.coupon.condition,"price":obj.coupon.price,"start_time":obj.coupon.start_time,"end_time":obj.coupon.end_time}
    class Meta:
        model = models.UserCoupon
        fields = '__all__'
class OrderDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.OrderDetailModel
        fields = '__all__'
import json
#订单的序列化
class OrderSerializer(serializers.ModelSerializer):
    #orderdetailmodel_set=OrderDetailSerializer(many=True)
    courselist=serializers.SerializerMethodField(read_only=True)
    def get_courselist(self,obj):
        course=[]
        all=obj.orderdetailmodel_set.all()
        for i in all:
            course.append({"id":i.id,"course":i.course.id,"cname":i.cname,"price":i.price,"discount":i.discount,"cover":i.cover})
        return course
    class Meta:
        model = models.OrderModel
        fields = '__all__'

class MyCourseSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.MyCourseModel
        fields = '__all__'